#coding: utf8
import urlparse
import time
import os.path
from scrapy import log
from decimal import *
from scrapy.http import Request
from scrapy.http import FormRequest
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from fresh.TwitterFbDB import TwitterFbDB
import datetime
import MySQLdb
import sys
from fresh.utils import Utils

class TwitterFbSearchMain(BaseSpider):

    name = "twfbSearchMain"
    db = TwitterFbDB()
    dictURL_Rest={}
    utils = Utils()

    def __init__(self, *args, **kwargs):
        super(TwitterFbSearchMain, self).__init__()
        reload(sys)
        sys.setdefaultencoding('utf-8')
        time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        filename = time + '.log'
        filename = os.path.join('logTwitterFbMain/',filename)
        log.start(logfile=filename,loglevel=log.DEBUG)
        self.closespider_errorcount = 100
        self.closespider_pagecount = 100
        self.closespider_exceptioncount = 100
        self.exception_type_count = 100

        if (self.db.connectDB()):
            """Read list of restaurants"""
            self.db.readURLs(self.start_urls,self.dictURL_Rest)
 
    def parse(self, response):
        try:
            """from scrapy.shell import inspect_response
            inspect_response(response)"""
            
            hxs = HtmlXPathSelector(response)
            links = hxs.select('//@href').extract()

            user_tw=''
            user_fb=''
            listTW = ['']
            listFB = ['']
            bUpdate=False            
            for link in links:
                slink = str(link)
                if slink.find('twitter')!=-1:
                    listTW.append(slink)
                if slink.find('facebook') != -1:
                    listFB.append(slink)
                    
            if len(listTW) == 2:
                user_tw = self.utils.getTwitterAccount(log,listTW[1])
                if len(user_tw)>0:
                    bUpdate=True
                    log.msg("twitter found: " + user_tw,level=log.DEBUG)
            
            if len(listFB) == 2:
                log.msg("abans: " + listFB[1],level=log.DEBUG)
                user_fb = self.utils.getFaceBookAccount(log,listFB[1])
                log.msg( "despres: " + user_fb,level=log.DEBUG)
                if len(user_fb)>=0:
                    bUpdate=True
                    log.msg("facebook found: " + user_fb,level=log.DEBUG)
                    
            if bUpdate:
                id_rest= self.dictURL_Rest.get(response.url)
                if id_rest is not None:
                    self.db.insertTwFb(id_rest,user_tw,user_fb)
                else:
                    """big problem! url not in dictionary!"""
                    log.msg( "BIG PROBLEM:--------------",level=log.DEBUG)
                    log.msg(str(response),level=log.DEBUG)
            return

        except Exception,e:
            log.msg("Error en parse web para twitter y FB: " + str(e),level=log.ERROR)
        